package com.bawei.mall.product.service.impl;

import com.bawei.common.core.domain.R;
import com.bawei.mall.product.domain.MallProductAdvertise;
import com.bawei.mall.product.domain.MallProductBrandInfo;
import com.bawei.mall.product.domain.MallProductHome;
import com.bawei.mall.product.domain.MallProductInfo;
import com.bawei.mall.product.domain.Vo.MallProductInfoVo;
import com.bawei.mall.product.mapper.MallProductAdvertiseMapper;
import com.bawei.mall.product.mapper.MallProductBrandInfoMapper;
import com.bawei.mall.product.mapper.MallProductInfoMapper;
import com.bawei.mall.product.service.MallProductHomeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Service
public class MallProductHomeInfoServiceImpl implements MallProductHomeInfoService {
    @Autowired
    private MallProductBrandInfoMapper mallProductBrandInfoMapper;
    @Autowired
    private MallProductAdvertiseMapper mallProductadvertiseMapper;
    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;

    @Override
    public R<MallProductHome>  content() {

        MallProductHome result = new MallProductHome();

        //异步处理
        CompletableFuture<List<MallProductInfoVo>> cf1 = CompletableFuture.supplyAsync(() -> {
            //商品信息
            return mallProductBrandInfoMapper.count();
        });

        CompletableFuture<List<MallProductAdvertise>> cf2 = CompletableFuture.supplyAsync(() -> {
            //广告信息
            return  mallProductadvertiseMapper.selectMallProductAdvertiseList(new MallProductAdvertise());
        });

        CompletableFuture<List<MallProductInfo>> cf3 = CompletableFuture.supplyAsync(() -> {
            //广告信息
            return  mallProductInfoMapper.selectList();
        });
        CompletableFuture<List<MallProductInfo>> cf4 = CompletableFuture.supplyAsync(() -> {
            //广告信息
            return  mallProductInfoMapper.selectListCommend();
        });


        try {
            List<MallProductInfoVo> mallProductInfoVos = cf1.get();
            result.setBrandList(mallProductInfoVos);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        try {
            List<MallProductAdvertise> mallProductAdvertises = cf2.get();
            result.setAdvertiseList(mallProductAdvertises);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        try {
            List<MallProductInfo> mallProductInfos = cf3.get();
            result.setNewProductList(mallProductInfos);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        try {
            List<MallProductInfo> mallProductInfos = cf4.get();
            result.setHotProductList(mallProductInfos);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        return R.ok(result);
    }





}
